<?php

namespace App\Http\Models;

use App\Libs\Bos\BosClientProxy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;

class BosModel extends Model
{
    /**
     * @var string
     */
    protected $bucket = 'toosound-website';

    /**
     * @var string
     */
    protected $bosKeyPrefix = 'dir-unknown/';

    /**
     * @param UploadedFile $uploadFile
     * @return string
     * @throws \Exception
     */
    public function uploadImage(UploadedFile $uploadFile)
    {
        $timeStamp = '_' . date('Y_m_d');
        $imgOriginName = $uploadFile->getClientOriginalName();
        $imgNameArr = explode(".", $imgOriginName);
        $imgType = $imgNameArr[1];
        $bosKey = $this->bosKeyPrefix . Hash::make($imgNameArr[0]) . $timeStamp . '.' . $imgType;

        $bosClient = new BosClientProxy();
        $bosClient->client->putObjectFromFile($this->bucket, $bosKey, $uploadFile->getRealPath());

        return $bosKey;
    }

    /**
     * @param $uploadFile
     * @param $bosKeyPrefix
     * @return string
     * @throws \Exception
     */
    public function uploadImages(UploadedFile $uploadFile, $bosKeyPrefix)
    {
        $timeStamp = '_' . date('Y_m_d');
        $imgOriginName = $uploadFile->getClientOriginalName();
        $imgNameArr = explode(".", $imgOriginName);
        $imgType = $imgNameArr[1];
        $bosKey = $bosKeyPrefix . time() . $timeStamp . '.' . $imgType;

        $bosClient = new BosClientProxy();
        $bosClient->client->putObjectFromFile($this->bucket, $bosKey, $uploadFile->getRealPath());

        return $bosKey;
    }

    /**
     * @param $file_path
     * @param $bos_key
     * @return mixed
     * @throws \Exception
     */
    public function uploadLocalFile($file_path, $bos_key)
    {
        $bosClient = new BosClientProxy();

        $bosClient->client->putObjectFromFile($this->bucket, $bos_key, $file_path);

        return $bos_key;
    }

    /**
     * @param $bosKey
     * @return mixed
     */
    public function getStaticUrl($bosKey)
    {
        return Arr::first(explode('?', $this->getUrl($bosKey)));
    }

    /**
     * @param $bosKey
     * @return string
     */
    public function getUrl($bosKey)
    {
        if (is_int(strpos($bosKey, 'http'))) return $bosKey;

        $bosClient = new BosClientProxy();
        $imgURL = $bosClient->client->generatePreSignedUrl($this->bucket, $bosKey, $bosClient->signArray);

        return $imgURL;
    }

    /**
     * @param $column_names
     * @throws \Exception
     */
    public function deleteBosColumns($column_names)
    {
        if (! is_array($column_names)) {
            throw new \Exception('column_names need to be an array');
        }

        array_map([$this, 'deleteBosColumn'], $column_names);
    }

    /**
     * @param $column_name
     * @return bool|null|string
     * @throws \Exception
     */
    public function deleteBosColumn($column_name)
    {
        if (! $column_name || ! is_string($column_name)) {
            throw new \Exception('column_name need to be an column name');
        }

        if (! $this->attributes[$column_name]) return null;

        return $this->deleteBosKey($this->attributes[$column_name]);
    }

    /**
     * @param $key
     * @return bool|string
     */
    public function deleteBosKey($key)
    {
//        $bosClient = new BosClientProxy();
//        return $bosClient->client->deleteObject($this->bucket, $key);
        $bosClient = new BosClientProxy();
        try {
            $bosClient->client->deleteObject($this->bucket, $key);
        } catch (\Exception $e) {
            return 'no key or something';
        }

        return true;
    }

    /**
     * @param $column_names
     * @throws \Exception
     */
    public function deleteBosJsonColumns($column_names)
    {
        if (! is_array($column_names)) {
            throw new \Exception('column_names need to be an array');
        }

        array_map([$this, 'deleteBosJsonArr'], $column_names);
    }


}
